package model;


import java.io.File;


public class TestScript {
	private String jobID;
	private String jobName;
	private String scriptName;
	private String scriptPath;
	private String log;
	private String dataStore;
	private TestResult result;
	private String machine;
	private int state;
    private String buildName;
    private String uniqueName;

	private ScriptExecutionThread thread;

	public final static int STATE_READY = 0;
	public final static int STATE_RUNNING = 1;
	public final static int STATE_COMPLETED_OK = 2;
	public final static int STATE_COMPLETED_ERR = 3;


    public String getMachine(){
        return this.machine;
    }

    public void setMachine(String machine){
        this.machine = machine;
    }

    public void setBuildName(String name){
        this.buildName = name;
    }

    public String getBuildName(){
        return this.buildName;
    }

    public boolean isExecuted(){
        File log = new File(getServerSideLogPath());
        return log.exists();
    }


    //Sample of the script: IN.negtive.cancelBeforeCopyFiles
	public TestScript(String scriptName, String machine, String buildName) throws InvalidVMHostNameException{
		state = STATE_READY;
        VMMachine m = VMMachineFactory.getVMMachine(machine);
//        this.scriptName = this.scriptPath.substring(this.scriptPath.lastIndexOf("\\")+1, this.scriptPath.length());
//        this.jobName = "TS_"+this.scriptName;
        this.scriptName = scriptName;
        this.machine = machine;
        this.buildName = buildName;
//        this.buildName = ConfigManager.getBuildName();
        this.scriptPath = ConfigManager.getProjectPath()+"\\"+this.scriptName.replace(".", "\\");
        this.uniqueName = this.scriptName+"["+this.machine+"]"+this.buildName;
        this.log = getDefaultLogPath();
	}

    public String getScriptName(){
        return this.scriptName;
    }

	public void run() {
		state = STATE_RUNNING;
		if(thread==null)
			thread = new ScriptExecutionThread(this);
		thread.start();
	}

    public String getJobName(){
        return this.jobName;
    }


	public void update() {
        String state = this.thread.getState().toString();
        if(state.equals(Thread.State.TERMINATED))
            this.state = STATE_COMPLETED_OK;
	}



    public void setState(int status){
        this.state = status;
    }

    public String getLogPath(){
        return this.log;
    }

    public String getServerSideLogPath(){
        return ConfigManager.getLogPath()+"\\"+this.uniqueName;
    }

    private String getDefaultLogPath() {
        String relativePath = this.scriptPath.replace(ConfigManager.getProjectPath(), "");
        relativePath = relativePath.replace("/", "\\").replace("\\", ".");
        if(relativePath.startsWith(".")){
            relativePath = "\\"+relativePath.substring(1,relativePath.length());
        }else{
            relativePath = "\\"+relativePath;
        }
        return ConfigManager.getProjectPath()+"_logs"+relativePath;
    }


	private String getJobID() {
        if(jobID==null){
            this.jobID = STAXJobLister.queryJobID(this.jobName);
        }
		return this.jobID;
	}

	public int getState() {
		return state;
	}

	public TestResult getResult() {
		if (this.getState() == STATE_COMPLETED_OK) {
			result = new TestResult(getServerSideLogPath());
			return result;
		} else
			return null;
	}

	public String getScriptPath(){
		return this.scriptPath;
	}


}

